import { request } from '../../request/request.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cart: [],
        address: {},
        totalPrice: 0,
        count: 0,
        token: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
    },

    //获取购物车信息
    getCartList() {
        let cart = wx.getStorageSync("goodCar") || [];
        cart = cart.filter(item => item.checked)
        this.setData({
            cart
        })
    },
    //获取收货地址
    handlerChooseAddress() {
        wx.getSetting({
            success: (result) => {
                let address = result.authSetting["scope.address"]
                if (address === true || address === undefined) {
                    wx.chooseAddress({
                        success: (result1) => {
                            wx.setStorageSync("address", result1);
                        },
                    });

                } else {
                    wx.openSetting({
                        success: (result3) => {
                            wx.chooseAddress({
                                success: (result2) => {
                                    wx.setStorageSync("address", result2);
                                },
                            });
                        },
                    });

                }

            },
        });
    },

    //合计
    getCountPrice() {
        let { cart } = this.data || []
        let flag = 0
        let totalPrice = cart.reduce((pre, next) => {
            if (next.checked) {
                flag++
                return pre + next.goods_price * next.num
            } else {
                return pre
            }

        }, 0)
        this.setData({
            totalPrice,
            count: flag
        })
    },

    //结算
    jiesuan() {
        let { address, count } = this.data
        if (!address) {
            wx.showToast({
                title: '未地址选择地址',
                icon: 'none',
                duration: 1500,
            });
            return
        }
        if (!count) {
            wx.showToast({
                title: '购物车为空',
                icon: 'none',
                duration: 1500,
            });
            return
        }
        wx.navigateTo({
            url: '/pages/pay/index',
        });

    },
    //用户信息
    async jiesuan() {
        let { token } = this.data
        if (token) {

        } else {
            let userInfo = await this.getInfo()
            let { signature, rawData, iv, encryptedData } = userInfo
            let { code } = await this.login()
            let result = await request({ url: 'users/wxlogin', data: { signature, rawData, iv, encryptedData, code }, method: 'post' })
            wx.showToast({
                title: result.data.meta.msg,
                icon: 'none',
                duration: 2500,
                mask: false,
            });

        }
    },
    getInfo() {
        return new Promise((res, rej) => {
            wx.getUserInfo({
                timeout: 10000,
                success: (result) => {
                    res(result)
                },
            });
        }).then(res => {
            return res
        }).catch(err => {
            wx.showToast({
                title: err,
                duration: 1500,
                mask: false,
            });

        })
    },

    //登录
    login() {
        return new Promise((res, rej) => {
            wx.login({
                timeout: 10000,
                success: (result) => {
                    res(result)
                },
            });
        })


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getCartList()
        let address = wx.getStorageSync("address");
        let token = wx.getStorageSync("token");
        this.setData({
            address,
            token
        })
        this.getCountPrice()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})